package com.xoa.controller.crm;


import com.xoa.dao.common.SysCodeMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.crm.CustomerWithBLOBs;
import com.xoa.model.crm.Order;
import com.xoa.service.crm.OrderService;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qiyang on 2019/6/3.
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private SysCodeMapper sysCodeMapper;

    //映射主页面
    @RequestMapping("/orderIndex")
    public String orderIndex(HttpServletRequest request) {
        return "/app/crm/order/orderIndex";
    }
    //映射编辑页面
    @RequestMapping("/updateOrder")
    public String updateOrder(HttpServletRequest request) {
        return "/app/crm/order/updateOrder";
    }
    //映射添加页面
    @RequestMapping("/addOrders")
    public String newLandResources() {
        return "app/crm/order/addOrder";
    }
    //映射添加页面
    @RequestMapping("/customerList")
    public String customerList() {
        return "app/crm/order/customerList";
    }
    //导入映射
    @RequestMapping("/orderInser")
    public String orderInser() {
        return "app/crm/order/orderInser";
    }
     //选择客户id页面
    @RequestMapping("/customerId")
    public String addcustomerId() {
            return "app/crm/order/addCustomerId";
    }
    //选择合同id页面
    @RequestMapping("/contractId")
    public String addContractId() {
        return "app/crm/order/addContractId";
    }
    //订单详情页面
    @RequestMapping("/orderDetails")
    public String orderDetails() {
        return "app/crm/order/orderDetails";
    }
    //选择产品id页面
    @RequestMapping("/productId")
    public String addProductId() {
        return "app/crm/order/addProductId";
    }
    //修改客户名称
    //选择客户id页面
    @RequestMapping("/updateCustomerId")
    public String updateCustomerId() {
        return "app/crm/order/updateCustomerId";
    }
    //处理时间  将前台传过来的时间存到数据库中
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
    /**
     *查询付款方式
     */
    @RequestMapping("selectByPayment")
    @ResponseBody
    public ToJson<Integer> selectBySource(HttpServletRequest request){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            // List<CustomerWithBLOBs> data = customerService.selectBySource();
            List<SysCode> sysCodes=sysCodeMapper.getSysCode("PAYMENTMETHOD");
            if(sysCodes!=null){
                json.setObj(sysCodes);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询货币单位
     * @param request
     * @return
     */
    @RequestMapping("selectMonetaryUnit")
    @ResponseBody
    public ToJson<Integer> selectByCurrency(HttpServletRequest request){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            // List<CustomerWithBLOBs> data = customerService.selectBySource();
            List<SysCode> sysCodes=sysCodeMapper.getSysCode("MONETARYUNIT");
            if(sysCodes!=null){
                json.setObj(sysCodes);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 分页 模糊查询
     */
    @RequestMapping("/insearchOrder")
    @ResponseBody
    public ToJson<Order> insearchOrder(HttpServletRequest request, Integer page, Integer pageSize, Boolean useFlag, Order order){
        Map<String,Object> map= new HashMap<String, Object>();
        order.setIsDeleted(Byte.valueOf("0"));
        map.put("obj",order);
        ToJson json =new ToJson<Order>(1,"err");
        String createdTimeStr=request.getParameter("createdTimeStr");
        map.put("createdTimeStr",createdTimeStr);
        try {
            List<Order> data =orderService.findAll(page, pageSize, useFlag, map);
            if(data!=null){
                PageParams pageParams = (PageParams) map.get("page");
                json.setTotleNum(pageParams.getTotal());
                json.setObj(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 添加订单
     * @param request
     * @param order
     * @return
     */
    @RequestMapping("addOrder")
    @ResponseBody
    public ToJson<Integer> addOrder(HttpServletRequest request, Order order){
        ToJson json = new ToJson<>(1, "err");
        try {
            Integer data= orderService.addOrder(order);
            if (data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 删除客户
     * @param request
     * @param orderId
     * @return
     */
    @RequestMapping("deleteOrder")
    @ResponseBody
    public ToJson<Integer> deleteByPrimaryKey(HttpServletRequest request, Integer orderId){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            Integer data =orderService.deleteByPrimaryKey(orderId);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 编辑回显
     * @param request
     * @param orderId
     * @return
     */
    @RequestMapping("/selectOrderById")
    @ResponseBody
    public ToJson<Order> selectByPrimaryKey(HttpServletRequest request, Integer orderId){
        ToJson json =new ToJson<CustomerWithBLOBs>(1,"err");
        try {
            Order data =orderService.selectByPrimaryKey(orderId);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 编辑订单
     */
    @RequestMapping("updateByPrimaryKey")
    @ResponseBody
    public ToJson<Integer> updateByPrimaryKey(HttpServletRequest request, Order order){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            Integer data =orderService.updateByPrimaryKey(order);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 导出
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/orderExport", produces = {"application/json;charset=UTF-8"})
    public ToJson<Order> queryExportUsers(HttpServletRequest request, HttpServletResponse response) {
        return orderService.orderExport(request,response);
    }

    /**
     * 导入
     * @param request
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/orderImport", produces = {"application/json;charset=UTF-8"})
    public ToJson<Order> insertImport(HttpServletRequest request, MultipartFile file) {
        return orderService.orderImport(request,file);
    }
}
